package com.demo.lcgl.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.demo.lcgl.controller.base.BaseController;
import com.demo.lcgl.entity.ExpenditureManagement;
import com.demo.lcgl.service.ExpenditureManagementService;

/**
 * 支出管理：(ExpenditureManagement)表控制层
 */
@RestController
@RequestMapping("/expenditure_management")
public class ExpenditureManagementController extends BaseController<ExpenditureManagement, ExpenditureManagementService> {

    /**
     * 支出管理对象
     */
    @Autowired
    public ExpenditureManagementController(ExpenditureManagementService service) {
        setService(service);
    }

    @PostMapping("/add")
    @Transactional
    public Map<String, Object> add(HttpServletRequest request) throws IOException {
        Map<String, Object> paramMap = service.readBody(request.getReader());
        this.addMap(paramMap);
        String sql = "SELECT MAX(expenditure_management_id) AS max FROM " + "expenditure_management";
        Integer max = service.selectBaseCount(sql);
        sql =
                "UPDATE `family_account` INNER JOIN `expenditure_management` ON family_account.account=expenditure_management.account SET "
                        + "family_account.balance = family_account.balance - expenditure_management.expenditure_amount WHERE expenditure_management"
                        + ".expenditure_management_id="
                        + max;
        service.updateBaseSql(sql);
        return success(1);
    }

}
